﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace Welcome.Dialog
{
    public partial class Save : Form
    {
        //客户区移动
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        public Save()
        {
            InitializeComponent();
            init();
        }
        //初始化
        public void init()
        {
            //窗口样式
            this.FormBorderStyle = FormBorderStyle.None;
            this.StartPosition = FormStartPosition.CenterScreen; //居中
            this.TopMost = true;
        }
        //客户区移动
        private void Save_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);//客户区移动
        }
        //保存
        private void button2_Click(object sender, EventArgs e)
        {
            string str = CreateFolder.xmlsavepath;
            XmlUtil a = new XmlUtil();
            a.TreeToXML(CreateFolder.treenode, str);
            if (File.Exists(str))
            {
                MessageBox.Show("保存成功 路径:" + str);
            }
            else
            {
                MessageBox.Show("保存失败");
            }    
        }
        //另存为
        private void button1_Click(object sender, EventArgs e)
        {
            string filepath = "";
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = ".xml|*.xml|所有文件|*.*";
            saveFileDialog.FilterIndex = 1;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.InitialDirectory = CreateFolder.exepath;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                filepath = saveFileDialog.FileName;
                XmlUtil a = new XmlUtil();
                a.TreeToXML(CreateFolder.treenode, filepath);
                if (File.Exists(filepath))
                {
                    MessageBox.Show("另存为成功 路径:" + filepath);
                }
                else
                {
                    MessageBox.Show("另存为失败");
                }
            }  
        }
        //关闭
        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //客户区移动
        private void Savepane_MouseDown(object sender, MouseEventArgs e)
        {
            //ReleaseCapture();
            //SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);//客户区移动
        }
    }
}
